﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UI;

namespace Room
{
    public class RoomUnionFightCheckPvp
    {
        public RoomBase roomBase;
        public RoomNetUnionFight net;
        public RoomCharactor selfCharactor;
        public Dictionary<long, RoomCharactor> rivalCharactors = new Dictionary<long, RoomCharactor>();
        public long willFightUuid = 0L;
        private bool currentGetTarget = false;

        public float fightDis = 0.5f;
        public float checkTime = 0.5f;
        public float currTimer = 0f;

        public void OnMainCharactorLoaded()
        {
            selfCharactor = roomBase.charactorManager.GetSelfCharactor();
        }
        
        public void OnLoadCompleteCharactor(RoomCharactor charactor)
        {
            var charactorName = charactor.name;
            var uuid = 0L;
            if (long.TryParse(charactorName,out uuid))
            {
                if (charactor.type == CharactorType.UnionFightOther)
                {
                    var ai = charactor.ai as RCAIUnionActor;
                    if (net.netBattle.camp != ai.Camp)
                    {
                        rivalCharactors[uuid] = charactor;
                    }
                }
            }
        }

        public void OnUnloadCompleteCharactor(string uuid)
        {
            var _uuid = 0L;
            if (long.TryParse(uuid, out _uuid))
            {
                if (rivalCharactors.ContainsKey(_uuid))
                {
                    rivalCharactors.Remove(_uuid);
                }
            }
        }

        public void Init(RoomBase roomBase)
        {
            this.roomBase = roomBase;
            this.net = roomBase.netManager as RoomNetUnionFight;
            this.checkTime = (float)Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionFightCheckPvpTimeDelay;
            this.fightDis = (float)Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionFightCheckPvpDistance;
        }
        
        public void Enter()
        {
            rivalCharactors.Clear();
        }

        public void Tick()
        {
            if (UnionFightData.IsOnHook)
                return;

            currTimer += Time.deltaTime;
            if (currTimer >= checkTime)
            {
                GetWillFightCharactors();
                SendWillFightCharactors();
            }
        }

        public void Leave()
        {
            rivalCharactors.Clear();
        }


        void GetWillFightCharactors()
        {
            if(selfCharactor==null)
            {
                return;
            }
            currentGetTarget = false;
            float currMinDis = fightDis;
            var myPos = new Vector2(selfCharactor.transform.position.x, selfCharactor.transform.position.z);
            foreach (var kvp in rivalCharactors)
            {
                var rival = kvp.Value;
                var uuid = kvp.Key;
                if (rival != null)
                {
                    var ai = rival.ai as RCAIUnionActor;
                    if( ai == null 
                        || ai.CurrState == UnionActorState.Attack 
                        || ai.CurrState == UnionActorState.Death 
                        || ai.CurrState == UnionActorState.Defend 
                        || ai.CurrState == UnionActorState.Revive)
                    {
                        continue;
                    }

                    var rivalPos = new Vector2(rival.transform.position.x, rival.transform.position.z);
                    var dis = Vector2.Distance(myPos, rivalPos);
                    if (dis <= currMinDis)
                    {
                        currMinDis = dis;
                        willFightUuid = uuid;
                        currentGetTarget = true;
                    }
                }
            }
        }
        
        void SendWillFightCharactors()
        {
            if(currentGetTarget)
            {
                net.SendCSUFPlayerAskFight(willFightUuid);
                currentGetTarget = false;

                ///发送完成了重置时间
                currTimer = 0f;
            }
            
        }
    }
}